package com.cd.controller.login;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.cd.bean.User;
import com.cd.service.user.UserService;
import com.cd.utils.SessionUtil;

/**
 * 后台登录
 *@Author zhk
 *@Date 2018-1-12
 **/
@RequestMapping("/login")
@Controller
public class LoginController {

	@Autowired
	UserService userService;
	
	/**
	 * 去登录界面
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/login_toLogin")
	public ModelAndView toLogin() throws Exception {
		ModelAndView mv = new ModelAndView();
		//mv.addObject("msg", "login");
		mv.setViewName("admin/toLogin");
		return mv;
	}
	
	/**
	 * 管理员登录
	 * @param username
	 * @param password
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/login")
	public ModelAndView login(@RequestParam(value="username") String username,@RequestParam(value="password") String password) throws Exception {
		ModelAndView mv = new ModelAndView();
		User user = new User();
		user.setUSERNAME(username);
		user.setPASSWORD(password);
		user = userService.findByUsernameAndPwd(user);
		if(user != null){
			SessionUtil.getSession().setAttribute("user", user);
			SessionUtil.getSession().setAttribute("username", user.getUSERNAME());
			mv.setViewName("admin/index/frame/main");
		}else{
			mv.setViewName("admin/toLogin");
		}
		return mv;
	}
	
	
	
	
	
	
	
	
	/**
	 * 后台欢迎页
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/welcome")
	public ModelAndView welcome() throws Exception {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("admin/index/welcome");
		return mv;
	}
	
	/**
	 * 退出
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/logOut")
	public ModelAndView logOut(HttpSession session) throws Exception {
		ModelAndView mv = new ModelAndView();
		session.removeAttribute("username");
		session.removeAttribute("user");
		session.invalidate();
		mv.setViewName("admin/toLogin");
		return mv;
	}
	/**
	 * 后台首页通用顶部
	 */
	@RequestMapping("/adminTop")
	public String adminTop() throws Exception {
		
		return "admin/admin_top";
	}
	/**
	 * 系统首页（后台）
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/goMain")
	public String index() throws Exception {
		
		return "admin/index/frame/main";
	}
	/**
	 * 后台主界面top
	 * @return
	 */
	@RequestMapping("frameTop")
	public String frameTop(){
		return "admin/index/frame/top";	
	}
	/**
	 * 后台主界面左边菜单
	 * @return
	 */
	@RequestMapping("frameLeft")
	public String frameLeft(){
		return "admin/index/frame/left";	
	}
	/**
	 * 后台主界面右边
	 * @return
	 */
	@RequestMapping("frameRight")
	public String frameRight(){
		return "admin/index/frame/right";	
	}
	
}
